package com.example.controller;

import com.example.common.RequirePermission;
import com.example.common.Result;
import com.example.common.PageResult;
import com.example.dto.StockRecordQueryDTO;
import com.example.service.AdminStockService;
import com.example.vo.StockRecordVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 管理员库存管理控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/admin/stock")
@CrossOrigin
@RequiredArgsConstructor
public class AdminStockController {

    private final AdminStockService adminStockService;

    /**
     * 分页查询所有商品的入库出库记录
     */
    @GetMapping("/records/page")
    @RequirePermission("admin:stock:records:view")
    public Result<PageResult<StockRecordVO>> getStockRecordsPage(
            @RequestParam(defaultValue = "1") int current,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(required = false) String recordNo,
            @RequestParam(required = false) String productName,
            @RequestParam(required = false) String productCode,
            @RequestParam(required = false) String recordType,
            @RequestParam(required = false) String operatorName,
            @RequestParam(required = false) String startTime,
            @RequestParam(required = false) String endTime) {
        
        StockRecordQueryDTO queryDTO = new StockRecordQueryDTO();
        queryDTO.setPage(current);
        queryDTO.setSize(size);
        queryDTO.setRecordNo(recordNo);
        queryDTO.setProductName(productName);
        queryDTO.setProductCode(productCode);
        queryDTO.setRecordType(recordType);
        queryDTO.setOperatorName(operatorName);
        queryDTO.setStartTime(startTime);
        queryDTO.setEndTime(endTime);
        
        return adminStockService.getStockRecordsPage(queryDTO);
    }

    /**
     * 查询所有入库记录（管理员创建商品）
     */
    @GetMapping("/in-records/page")
    @RequirePermission("admin:stock:in:view")
    public Result<PageResult<StockRecordVO>> getInRecordsPage(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(required = false) String productName,
            @RequestParam(required = false) String productCode,
            @RequestParam(required = false) String startTime,
            @RequestParam(required = false) String endTime) {
        
        return adminStockService.getInRecordsPage(page, size, productName, productCode, startTime, endTime);
    }

    /**
     * 查询所有出库记录（店长入库的商品）
     */
    @GetMapping("/out-records/page")
    @RequirePermission("admin:stock:out:view")
    public Result<PageResult<StockRecordVO>> getOutRecordsPage(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(required = false) String productName,
            @RequestParam(required = false) String productCode,
            @RequestParam(required = false) String operatorName,
            @RequestParam(required = false) String startTime,
            @RequestParam(required = false) String endTime) {
        
        return adminStockService.getOutRecordsPage(page, size, productName, productCode, operatorName, startTime, endTime);
    }

    /**
     * 根据商品ID查询库存记录
     */
    @GetMapping("/records/product/{productId}")
    @RequirePermission("admin:stock:records:view")
    public Result<List<StockRecordVO>> getStockRecordsByProductId(@PathVariable Long productId) {
        return adminStockService.getStockRecordsByProductId(productId);
    }

    /**
     * 导出库存记录
     */
    @GetMapping("/records/export")
    @RequirePermission("admin:stock:records:export")
    public Result<List<StockRecordVO>> exportStockRecords(
            @RequestParam(required = false) String recordType,
            @RequestParam(required = false) String productName,
            @RequestParam(required = false) String startTime,
            @RequestParam(required = false) String endTime) {
        
        StockRecordQueryDTO queryDTO = new StockRecordQueryDTO();
        queryDTO.setRecordType(recordType);
        queryDTO.setProductName(productName);
        queryDTO.setStartTime(startTime);
        queryDTO.setEndTime(endTime);
        
        return adminStockService.exportStockRecords(queryDTO);
    }

    /**
     * 获取库存统计信息
     */
    @GetMapping("/statistics")
    @RequirePermission("admin:stock:records:view")
    public Result<Object> getStockStatistics(
            @RequestParam(required = false) String startTime,
            @RequestParam(required = false) String endTime) {
        
        return adminStockService.getStockStatistics(startTime, endTime);
    }
} 